package taller2.view.windows.MainWindows;

import java.awt.GridBagLayout;

import taller2.controller.listeners.WindowClosingListener;
import taller2.controller.listeners.evaluateMeButtons.EvaluateMeButtonListener;
import taller2.interfaces.AlgorithmApplicator;
import taller2.interfaces.ChangeableWindow;
import taller2.interfaces.Graph;
import taller2.model.factorys.GraphFactory;
import taller2.utils.Constants;
import taller2.utils.Point;
import taller2.utils.Utils;
import taller2.view.components.CharacterBox;
import taller2.view.draws.DrawingArea;
import taller2.view.draws.GraphDrawer;
import taller2.view.menus.CodeBrowser;
import taller2.view.menus.CodeMenu;
import taller2.view.menus.MainEdittingMenus.MainEdittingMenu;
import taller2.view.windows.MyWindow;

import javax.swing.JPanel;

public abstract class MainWindow implements ChangeableWindow{
	protected MyWindow window;
	protected CodeBrowser browser;
	protected CodeMenu menu;
	protected Graph graph;
	protected GraphDrawer drawer;
	protected DrawingArea area;
	protected MainEdittingMenu selector;
	protected AlgorithmApplicator algorithmApplicator;

	protected CharacterBox panelTexto;

	// constructor
	public MainWindow(MyWindow parent, String path,boolean directed, AlgorithmApplicator algorithmApplicator,EvaluateMeButtonListener evaluateMe){
		this.window = new MyWindow("window");
		this.window.setResizable(false);

		this.algorithmApplicator = algorithmApplicator;	
		
		crearGrafo(directed);
		
		this.drawer = new GraphDrawer(graph,Constants.selectedColor,Constants.unSelectedColor);	
	
		JPanel panelBrowsers = crearBrowsers(path);

		this.area = new DrawingArea(Constants.backgroundPath,drawer);
		this.selector = createMainEdittingMenu(evaluateMe);
				
		this.window.addComponent(area,0.5,1,new Point(0,0));
		this.window.addComponent(selector,0.5,0,new Point(0,1));
	
		this.window.addComponent(menu,0.5,0,new Point(1,1));
		this.window.addComponent(panelBrowsers,0.5,1,new Point(1,0));

		this.window.fullScreen();
		this.window.hideOnClose();
		
		WindowClosingListener closingListener = new WindowClosingListener(parent);
		closingListener.setBrowser(browser);
		closingListener.setMenu(menu);		
		this.window.addWindowListener(closingListener);
	}
	
	protected JPanel crearBrowsers (String path){
		
		JPanel panelBrowsers = new JPanel(new GridBagLayout());
		
		this.browser = new CodeBrowser(path,Constants.fontSize,this.algorithmApplicator,window, graph, false);
		Utils.addComponent(panelBrowsers,browser,1,1,new Point(0,0));
				
		this.menu = new CodeMenu(browser, panelTexto,Constants.fontSize,Constants.codePath);
		
		return panelBrowsers;
	}
	
	protected MainEdittingMenu createMainEdittingMenu(EvaluateMeButtonListener evaluateMe){
		return (new MainEdittingMenu(this, area,graph,this.algorithmApplicator,evaluateMe));
	}
	
	protected void crearGrafo(boolean directed){
		if(directed){
			this.graph = GraphFactory.directedGgraphInstance();
		}
		else{
			this.graph = GraphFactory.undirectedGraphInstance();
		}
	}
	
	// used to change how much of the screen goes to the drawing
	// and how much to the pseudocode
	public void rebalanceWindow(float tamArea){
		float tamCode = 1-tamArea;

		this.window.remove(area);
		this.window.remove(selector);
		this.window.remove(menu);
		
		this.window.addComponent(area,tamArea,1,new Point(0,0));
		this.window.addComponent(selector,tamArea,0,new Point(0,1));
		this.window.addComponent(menu,tamCode,0,new Point(1,1));

		this.window.revalidate();
		this.window.repaint();
	}
	
	// redraws the window
	public void redraw(){
		window.redraw();
	}
	
	public void aplicarCambios(Graph graph){
	}

	@Override
	public void setVisible(boolean b) {
		window.setVisible(b);
	}

	public CodeBrowser getBrowser() {
		return browser;
	}

	public CodeMenu getMenu(){
		return menu;
	}
}
